#!/bin/bash
pwd
echo "开始执行脚本"

# git超级用户
name="chenhuasheng"
password="HUAsheng1478"
# 项目名称
PorjectName='MicroAppStore'
# 库名字
FMK_NAME='MicroAppStore'
# 文档地址
pluginUrl='https://sharp-aryabhata-14690b.netlify.com/guide'
# 1必选插件0非必选插件
chooseFlag='0'

# 1.获取Tag列表按时间排序
Tags=`git for-each-ref --sort=taggerdate --format '%(refname:short)'`
echo ${Tags}
#获取最新两个Tag得到版本号
tagA=`echo ${Tags} | awk '{print $(NF-1)}'`
tagB=`echo ${Tags} | awk '{print $NF}'`

echo ${tagA}
echo ${tagB}

#2.获取两个tag之间的commit
commits=`git log ${tagA}..${tagB} --pretty=oneline --pretty=format:"%s" --grep="issue:"`
echo "上一个Tag(${tagA})之后的commit:${commits}"

# 替换issue:为''
commits=`echo ${commits//issue:/}`
# 替换:为-
commits=`echo ${commits//:/\-}`
echo ${commits}

#urlencode
#commits=`python3 -c "import sys, urllib.parse as ul;print(ul.quote(\"${commits}\"))"`
#echo ${commits}

Tag=${tagB}
echo "准备发布版本: ${Tag}"

# 版本号
podspec_version_new='1.0.6-TEST'
if [ -n "$Tag" ]; then
    echo "Tag is not empty"
    podspec_version_new=$Tag
fi
echo ${podspec_version_new}
# 描述
describle=${commits}

#3.本地编译
buildDir=$(pwd)/build
if [ -d ${buildDir} ]
then
rm -rf ${buildDir}
fi

xcodebuild -derivedDataPath ${buildDir} -workspace ${PorjectName}.xcworkspace -scheme ${PorjectName} -configuration "Release" -sdk iphoneos clean build
if [ "$?" != "0" ]; then
    echo "\n ------ 本地编译失败 ------ \n"
    exit 1;
fi

buildFrameDir=${buildDir}/Build/Products/Release-iphoneos/${FMK_NAME}.framework
echo "\n ---- buildFrameDir: ${buildFrameDir}"

#4.创建frameworkDir目录,在新目录中clone framework
FrameDir=$(pwd)/frameworkDir
if [ -d ${FrameDir} ]
then
rm -rf ${FrameDir}
fi
mkdir -p ${FrameDir}
cd ${FrameDir}
gitUrl="http://${name}:${password}@e-git.yfb.sunline.cn/TinyBuilder/tiny-app/tiny-ios-framework/"
git clone --depth 1 $gitUrl$PorjectName

if [ "$?" != "0" ]; then
    echo "\n ------ framework clone 失败 ------ \n"
    exit 1;
fi

#5.复制本地build的framework 到 frameworkDir
frameworkDir=$(pwd)/${PorjectName}
echo "\n ---- frameworkDir: ${frameworkDir}"

rm -rf ${frameworkDir}/${FMK_NAME}.framework
cp -R "${buildFrameDir}" "${frameworkDir}"

#6.修改版本号
podspecFile=${frameworkDir}/${FMK_NAME}.podspec
echo "podspecFile ${podspecFile}"

# 查找 podspec 的版本
search_str="s.version"
# 读取podspec的版本
podspec_version=""

my_file="${podspecFile}"
echo "myfile ${my_file}"
while read my_line
do
# 查找到包含的内容，正则表达式获取以 ${search_str} 开头的内容
result=$(echo ${my_line} | grep "^${search_str}")
if [[ "$result" != "" ]]
then
echo "${my_line} 包含 ${search_str}"
# 分割字符串，是变量名称，不是变量的值; 前面的空格表示分割的字符，后面的空格不可省略
array=(${result// / })
# 数组长度
count=${#array[@]}
# 获取最后一个元素内容
version=${array[count - 1]}
# 去掉 '
version=${version//\'/}
podspec_version=$version
fi
done < $my_file

echo "podspec_version: ${podspec_version}"
sed -i "" "s/${podspec_version}/\"${podspec_version_new}\"/g" ${podspecFile}

#7.pod 本地校验
echo "执行 pod 本地校验"
#进入新clone的framwork目录
cd ${frameworkDir}
pwd
/usr/local/bin/pod lib lint --sources='http://e-git.yfb.sunline.cn/TinyBuilder/tiny-app/tiny-spec/tiny-spec.git,https://github.com/CocoaPods/Specs.git' --allow-warnings --verbose

if [ "$?" != "0" ]; then
    echo "pod 本地校验失败"
    exit 1;
fi

#8.推送git
echo "执行 git 打标签tag，并推送到远端"
echo "git add ."
git add .
echo "git status"
git status
echo "git commit -m ${podspec_version_new}"
git commit -m ${podspec_version_new}
# 执行 git 打标签tag,推送到远端
echo "git tag ${podspec_version_new}"
git tag ${podspec_version_new}
git push origin master --tags

#9.pod 远端校验
echo "执行 pod 远端校验"
echo "pod spec lint --allow-warnings --verbose"
/usr/local/bin/pod spec lint ${podspecFile} --sources='http://e-git.yfb.sunline.cn/TinyBuilder/tiny-app/tiny-spec/tiny-spec.git,https://github.com/CocoaPods/Specs.git' --allow-warnings

echo "执行 pod 发布"
#10.发布私有库
/usr/local/bin/pod repo push sunline-tinybuilder-tiny-app-tiny-spec-tiny-spec ${podspecFile} --allow-warnings
# ../../../jenkinsShell/podRepo.exp ${podspecFile}


if [ "$?" != "0" ]; then
    echo "pod 发布失败"
    exit 1
else
    #回到根目录
    cd ../..
    # 用python对字符串encode
    eDescribe=`python3 -c "import sys, urllib.parse as ul;print(ul.quote('${describle}'))"`
    echo ${describle}
    eDependencies=`python3 -c "import sys, urllib.parse as ul;print(ul.quote('[\"${FMK_NAME},${podspec_version_new}\"]'))"`
    echo ${dependencies}

    URL="https://p.mtiny.cn:9442/api/api/tiny_dev_plugin_history_list.tml?flow=addtiny"
    URL=${URL}"&appType=1"
    URL=${URL}"&pluginName=${PorjectName}"
    URL=${URL}"&version=${podspec_version_new}"
    URL=${URL}"&describe=${eDescribe}"
    URL=${URL}"&dependencies="${eDependencies}
    #1必选0非必选
    URL=${URL}"&flag=${chooseFlag}"
    URL=${URL}"&pluginUrl=${pluginUrl}"

    echo "请求的url是$URL"
    #curl 请求
    info=`curl -s -m 10 --connect-timeout 10 -I $URL`

    #获取返回码
    code=`echo $info|grep "HTTP"|awk '{print $2}'`
    #对响应码进行判断
    if [ "$code" == "200" ];then
        echo "请求成功，响应码是$code"
    else
        echo "请求失败，响应码是$code"
    fi
fi
